from django.test import TestCase
from django.test import Client


class InterviewTestCase(TestCase):
    def test_interview(self):
        c = Client()
        r = c.post('/upload/', {'name': '客户端1', 'score': 999999}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '上传成功'})

        r = c.post('/upload/', {'name': '客户端2', 'score': 950012}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '上传成功'})

        r = c.post('/upload/', {'name': '客户端3', 'score': 923333}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '上传成功'})

        r = c.post('/upload/', {'name': '客户端4', 'score': 544544}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '上传成功'})

        r = c.post('/upload/', {'name': '客户端5', 'score': 345452}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '上传成功'})

        r = c.post('/upload/', {'name': '客户端5', 'score': 345453}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '上传成功'})

        r = c.post('/upload/', {'name': '客户端5'}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '数据异常'})

        r = c.post('/upload/', {'score': 345452}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '数据异常'})

        r = c.post('/upload/', {}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '数据异常'})

        r = c.post('/upload/', {
            'name': '客户端5aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
            'score': 345452}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '客户端号超长'})

        r = c.post('/upload/', {'name': '客户端5', 'score': 0}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '分数错误'})

        r = c.post('/upload/', {'name': '客户端5', 'score': 10000000}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '分数错误'})

        r = c.post('/query/', {'start': 1, 'end': 3}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '数据异常'})

        r = c.post('/query/', {'name': '客户端5', 'end': 3}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '数据异常'})

        r = c.post('/query/', {'name': '客户端5', 'start': 1}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '数据异常'})

        r = c.post('/query/', {}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '数据异常'})

        r = c.post('/query/', {'name': '客户端5', 'start': 3, 'end': 1}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '排名不对'})

        r = c.post('/query/', {'name': '客户端5', 'start': 1, 'end': 3}, content_type='application/json')
        self.assertEqual(r.status_code, 200)
        self.assertDictEqual(r.json(), {'message': '查询成功',
                                        'result': [{'name': '客户端1', 'ranking': 1, 'score': 999999},
                                                   {'name': '客户端2', 'ranking': 2, 'score': 950012},
                                                   {'name': '客户端3', 'ranking': 3, 'score': 923333},
                                                   {'name': '客户端5', 'ranking': 5, 'score': 345453}]})

# import requests

# r = requests.post('http://127.0.0.1:8000/upload/', json={'name': '客户端1', 'score': 999999})
# print(r.json())
#
# r = requests.post('http://127.0.0.1:8000/upload/', json={'name': '客户端2', 'score': 950012})
# print(r.json())
#
# r = requests.post('http://127.0.0.1:8000/upload/', json={'name': '客户端3', 'score': 923333})
# print(r.json())
#
# r = requests.post('http://127.0.0.1:8000/upload/', json={'name': '客户端4', 'score': 544544})
# print(r.json())
#
# r = requests.post('http://127.0.0.1:8000/upload/', json={'name': '客户端5', 'score': 345452})
# print(r.json())

# r = requests.post('http://127.0.0.1:8000/query/', json={'name': '客户端5', 'start': 1, 'end': 3})
# print(r.json())
